package com.duleme.servlet;

import com.duleme.common.JsonUtils;
import com.google.gson.Gson;
import com.duleme.dao.UserInfoDao;
import com.duleme.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "certificate",urlPatterns = "/certificate")
public class CertificationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        resp.setCharacterEncoding("UTF-8");
        String name=req.getParameter("realName");
        String id=req.getParameter("idNumber");
        User user=(User)req.getSession().getAttribute("user");
        System.out.println("zhuce:"+user.getUserName());
        System.out.println("zhuce:"+user.getUserId());
        //User user=new User();
        //user.setUserId(user.getUserId());
        //UserInfo userInfo=(UserInfo)req.getSession().getAttribute("userInfo");
        UserInfoDao dao=new UserInfoDao();
        Gson gson=new Gson();
        if(dao.findById(id)!=null){
            System.out.println("身份证号已存在");
            resp.getWriter().print(JsonUtils.result2Json("0","info","身份证号已存在"));
        }else{
            if(dao.updateIdAndName(user.getUserId(),id,name)){
                resp.getWriter().print(JsonUtils.result2Json("1","info","认证成功"));
            }else{
                resp.getWriter().print(JsonUtils.result2Json("0","info","未知原因"));
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
